package com.lkd.handler;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lkd.business.MsgHandler;
import com.lkd.config.TopicConfig;
import com.lkd.contract.VendoutContract;
import com.lkd.contract.VendoutResultContract;
import com.lkd.emq.Topic;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
@Topic(TopicConfig.VMS_RESULT_TOPIC)
public class VendoutResultHandler implements MsgHandler {
    @Autowired
    private VendingMachineService vendingMachineService;

    @Override
    public void process(String jsonMsg) throws IOException {
        log.info("出货结果，设备端返回的消息：{}",jsonMsg);
        VendoutResultContract vendoutResultContract = JsonUtil.getByJson(jsonMsg, VendoutResultContract.class);
        if (vendoutResultContract == null || StringUtils.isBlank(vendoutResultContract.getInnerCode())){
            log.info("传入数据不存在或设备编号为空");
            return;
        }
        vendingMachineService.vendOutResult(vendoutResultContract);
    }
}
